﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using AllInOnePluginBL.Helpers;
using Rss;

namespace AllInOnePluginBL.SiteScrappers
{
    /// <summary>
    /// Summary description for SdarotNet
    /// </summary>
    public class SdarotNet
    {
        private const string DEFAULT_URL = "http://sdarot.net/category/tube";
        protected const string SEARCH_URL = "http://sdarot.net/?s={0}&x=0&y=0";

        public SdarotNet()
        {
            //this.Name = "Sdarot.Net";
        }

        #region Private Methods
        private RssFeed LoadCategories(string url)
        {
            try
            {
                var page = Utils.GetPage(url, Encoding.UTF8);

                const string categoriesSectionRegEx = "<a href=\"http://sdarot.net/category/tube\">סדרות לצפייה ישירה</a>(.+?)</ul>";
                var categoriesSection = Regex.Match(page, categoriesSectionRegEx, RegexOptions.Singleline).Groups[1].Value;

                const string categoriesLinkRegEx = "<a href=\"(.+?)\">(.+?)</a>";
                var matches = Regex.Matches(categoriesSection, categoriesLinkRegEx, RegexOptions.Singleline);
                foreach (Match match in matches)
                {
                    var categoryPageLink = match.Groups[1].Value;
                    var categoryTitle = match.Groups[2].Value;

                    //movie.MovieLinks.Add(categoryPageLink);

                    //var movieItems = movie.GetRssItems(Utils.GetSiteUrl(), this._pluginId, (int)PluginMode.MOVIES_MODE);
                    //foreach (var movieItem in movieItems)
                    //{
                    //    rssChannel.Items.Add(movieItem);
                    //}
                }

                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private RssFeed LoadABCSeries(string url)
        {
            try
            {
                var page = Utils.GetPage(url, Encoding.UTF8);

                const string abcSectionRegEx = "<h3 class=\"widget-title\"> כל הסדרות באתר א-ת</h3>.*?</div>";
                var categoriesSection = Regex.Match(page, abcSectionRegEx, RegexOptions.Singleline).Groups[0].Value;

                const string itemsSectionRegEx = "<a href=\"(.+?)\">(.+?)</a>";
                var matches = Regex.Matches(categoriesSection, itemsSectionRegEx, RegexOptions.Singleline);

                foreach (Match match in matches)
                {
                    var seriesPageLink = match.Groups[1].Value;
                    var seriesTitle = match.Groups[2].Value;

                    //movie.MovieLinks.Add(seriesPageLink);

                    //var movieItems = movie.GetRssItems(Utils.GetSiteUrl(), this._pluginId, (int)PluginMode.MOVIES_MODE);
                    //foreach (var movieItem in movieItems)
                    //{
                    //    rssChannel.Items.Add(movieItem);
                    //}
                }
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private RssFeed LoadMovies(string url)
        {
            try
            {
                var page = Utils.GetPage(url, Encoding.UTF8);

                var sessonMatches = page.Split(new[] { "<h5 class=\"toggle\">" }, StringSplitOptions.RemoveEmptyEntries);

                

                foreach (var match in sessonMatches)
                {
                    //const string sessionSectionRegEx = "<h5 class=\"toggle\"><a href.*?>(.+?)</a></h5>(.+?)";
                    //var sessonMatches = Regex.Matches(page, sessionSectionRegEx, RegexOptions.Singleline);

                    //var moviePageLink = match.Groups[1].Value;
                    //var movieTitle = match.Groups[2].Value;
                    //var imageLink = match.Groups[3].Value;
                    //var plot = match.Groups[4].Value;

                    //var movie = new Movie
                    //                {
                    //                    Name = movieTitle,
                    //                    ImageLink = imageLink,
                    //                    Plot = plot
                    //                };

                    //var pageContent = Utils.GetPage(moviePageLink, Encoding.UTF8);
                    //movie.MovieLinks = GetMovieLinks(pageContent);

                    //var movieItems = movie.GetRssItems(Utils.GetSiteUrl(), this._pluginId, (int)PluginMode.MOVIE_MODE);
                    //foreach (var movieItem in movieItems)
                    //{
                    //    //var rssCategory = new RssCategory { Name = "MegaVideo" };
                    //    //movieItem.Categories.Add(rssCategory);
                    //    rssChannel.Items.Add(movieItem);
                    //}
                }

                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private string GetNextPageLink(string pageContent)
        {
            try
            {
                const string nextPageExpression = "class=\"numeric_onclick\">.*?<a href=\"(.+?)\"";
                var exp = new Regex(nextPageExpression, RegexOptions.Singleline);
                var match = exp.Match(pageContent);
                if (match.Success)
                {
                    return match.Groups[1].Value.Trim();
                }
            }
            catch { }

            return string.Empty;
        }
        #endregion
    }
}